home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue51 / Clinic / IOErrorU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-09-02  |  1.6 KB  |  74 lines

  1. unit IOErrorU;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.DFM}
  24.  
  25. {$ifdef Ver90}
  26. //Not defined in Delphi 2
  27. procedure ShowMessageFmt(const Msg: string; Params: array of const);
  28. begin
  29.   ShowMessage(Format(Msg, Params));
  30. end;
  31. {$endif}
  32.  
  33. procedure BetterIOError(E: EInOutError);
  34. var
  35.   EIO: EInOutError;
  36. begin
  37.   if Assigned(E) then
  38.   begin
  39.     EIO := EInOutError.Create(E.Message);
  40.     EIO.ErrorCode := E.ErrorCode;
  41.     case E.ErrorCode of
  42.       1, 6..99: EIO.Message := Format('Win32 API error %d:'#13#13'%s',
  43.         [E.ErrorCode, SysErrorMessage(E.ErrorCode)]);
  44.       2..5, 100, 101, 106: { As descriptive as can be right now };
  45.       102: EIO.Message := 'File variable not assigned a name with AssignFile';
  46.       103: EIO.Message := 'File not open';
  47.       104: EIO.Message := 'File not open for input';
  48.       105: EIO.Message := 'File not open for output';
  49.       107..149: EIO.Message := Format('Delphi I/O error %d', [E.ErrorCode]);
  50.     end; //case
  51.     raise EIO
  52.   end
  53. end;
  54.  
  55. procedure TForm1.Button1Click(Sender: TObject);
  56. var
  57.   TF: TextFile;
  58. begin
  59.   AssignFile(TF, 'c:\SoemFile.Txt');
  60.   try
  61.     Reset(TF);
  62.     try
  63.       //Blah blah
  64.     finally
  65.       CloseFile(TF)
  66.     end //try..finally
  67.   except
  68.     on E: EInOutError do
  69.       BetterIOError(E)
  70.   end //try..except
  71. end;
  72.  
  73. end.
  74.